package com.wxn.learncompose.pagers

import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.compose.AsyncImagePainter
import coil.compose.SubcomposeAsyncImage
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.wxn.learncompose.R

@Composable
fun ImagePager() {
    Surface() {
        
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight(),
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            //指定边角为圆角
            Image(painterResource(id = R.drawable.wallpaper),
                contentDescription = "wallpaper",
                modifier = Modifier
                    .fillMaxWidth()
                    .height(260.dp)
                    .clip(RoundedCornerShape(14.dp))
            )
            Spacer(modifier = Modifier.height(12.dp))

            //裁剪成圆形显示，Crop形式
            Surface(
                shape = CircleShape,  //指定边框的形状
                border = BorderStroke(2.dp, Color.Green)  //通过Surface来指定边框颜色和宽度
            ) {

                Image(painterResource(id = R.drawable.wallpaper), contentDescription = null,
                    modifier = Modifier
                        .size(200.dp)
                        .clip(CircleShape),
                    contentScale = ContentScale.Crop)
            }

            Spacer(modifier = Modifier.height(12.dp))

            //通过coil的AsyncImage 来加载远程图片
            Surface {
                AsyncImage(model = ImageRequest.Builder(LocalContext.current)
                    .data("https://picsum.photos/300/300") //指定请求路径
                    .crossfade(true)   //使用淡入效果
                    .crossfade(250)
                    .build(),
                    contentDescription = "picsum",
                    modifier = Modifier.clip(RoundedCornerShape(5.dp)),
                    placeholder = painterResource(id = R.drawable.imgplaceholder),
                    error = painterResource(id = R.drawable.imgerror),
                    onSuccess = {
                        Log.d("ImagePager", "load image success.")
                    }
                    )
            }

            Spacer(modifier = Modifier.height(12.dp))

            //使用Coil的SubComposeAsyncImage 来加载图片，设置加载过程的进度条显示
            Surface {
                SubcomposeAsyncImage(model = ImageRequest.Builder(LocalContext.current)
                    .data("https://picsum.photos/300/300") //指定请求路径
                    .crossfade(true)   //使用淡入效果
                    .crossfade(250)
                    .build(),
                    contentDescription = stringResource(id = R.string.imgcontentdesc),
                    loading = {
                        CircularProgressIndicator()
                    },
                    )
            }
            Spacer(modifier = Modifier.height(12.dp))

            //使用 Coil的AsyncImagePainter 和Image 来展示图片
            Surface {
                var painter = rememberAsyncImagePainter(model = ImageRequest.Builder(LocalContext.current)
                    .data("https://picsum.photos/300/300")
                    .crossfade(true)
                    .crossfade(210).build())
                if (painter.state is AsyncImagePainter.State.Loading) {
                    CircularProgressIndicator()
                }
                Image(painter = painter,
                    contentDescription = stringResource(id = R.string.imgcontentdesc),
                    modifier = Modifier.padding(0.dp).width(200.dp).height(100.dp).clip(CircleShape),
                    contentScale = ContentScale.Crop)
            }

        }
    }
}